from flask import Blueprint,jsonify,request

from utils.db import db1

from comm import get_data

import json

from utils.redis_bp import r

work_bp = Blueprint('work',__name__)

@work_bp.route('/getworkflow')
def getworkflow():
    sql = "select * from workflow"

    res = db1.find_all(sql)

    return jsonify({'code':200,'list':res})



# 获取数据源接口
@work_bp.route('/getresourcedata')
def getresourcedata():
    # 首次先从数据库中取值
    values = r.get_to('resourcedata')

    print(values)

    if values:
        list = eval(values)

    else:
        sql = "select * from data_resource"
        res = db1.find_all(sql)
        list = []
        if res:
            for i in res:
                dict = {'id':i['id'],'name':i['name']}
                sql1 = "select id,name from %s" % (i['tablename'])

                res1 = db1.find_all(sql1)

                dict['mes'] = res1

                list.append(dict)

        r.set_to('dataresource',json.dumps(list))

    return jsonify({'code':200,'list':list})










@work_bp.route('/addworkflow',methods=["POST"])
def addworkflow():
    data = get_data()

    print(data)

    sql = "update workflow set params = '%s' where id = %d" % (data['list'],int(data['id']))

    db1.update(sql)

    return jsonify({'code':200,'mes':'添加成功'})





@work_bp.route('/adddevise',methods=["POST"])
def adddevise():
    data = get_data()

    print(data)

    sql = "select * from devise where user_id = %d" % int(data['userid'])

    res = db1.find_all(sql)

    if res:

        print(json.dumps(data))

        print(data)

        # sql = "insert into devise(id,title,params,status,user_id) values(0,'%s','%s',1,'%s','%s',1)" % (data['title'],data['params'],data['userid'])
        #
        # db1.update(sql)

    return jsonify({'code':200,'mes':'添加成功'})

